#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include <pthread.h>

#include "mosquitto.h"
#include "mqtt_client.h"


int main(int argc, char** argv)
{
    
    const char* host = "localhost";
    const char* port = "1883";
    char * topic = "/mqtt/test";
    const char* username = "admin";
    const char* password = "admin";

    const char* message = " hello mqtt! from rog.mqtt@";




    if (argv[1] != NULL)
        host = argv[1];
    
    if (argv[1] != NULL)
        port = argv[2];

    printf("host=argv[1] port=argv[2]\n");
    printf("argv[1]:%s argv[2]:%s\n", host, port);

    printf("\nis this not set will use default set\n");
    printf("host:%s port:%s\n ", host, port);
    

    if (client_subscribe_init_connect(host, port, username, password) != 0) {
        fprintf(stderr, " mqtt client initial connect and subscribe failed!\n");
    } else {
        fprintf(stdout, " mqtt client initial connect and subscribe successful!\n");
    }

    if (client_publish_init_connect(host, port, username, password) != 0)
        fprintf(stderr, " mqtt client initial connect and publish failed!\n");
    else
        fprintf(stdout, " mqtt client initial connect and publish successful!\n");

    

    // if (client_subscribe_connect_status()) {
    //     do {
    //         mqtt_client_subscribe(topic);
    //     } while (true);
    // }

    mqtt_client_subscribe(topic);

    if (mqtt_client_publish(topic, message) != 0)
        fprintf(stderr, " mqtt client publish to topic:\"%s\" failed!", topic);
    else
        fprintf(stdout, " mqtt client publish to topic:\"%s\" successful!\n", topic);

    while(true);

    /* client publish and subscribe resource */
    client_publish_release_resource();
    client_subscribe_release_resource();
}